package com.tedu.micro.service.service.impl;

import com.tarena.micro.service.pojo.param.OrderAddParam;
import com.tarena.micro.service.pojo.po.OrderPO;
import com.tedu.micro.service.mapper.OrderMapper;
import com.tedu.micro.service.service.CartService;
import com.tedu.micro.service.service.OrderService;
import com.tedu.micro.service.service.StockService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private StockService stockService;
    @Autowired
    private CartService cartService;
    @Override
    public Boolean addOrder(OrderAddParam param) {
        //1 扣减库存 param productCode 和 count
        Boolean stockResult = stockService.reduceStock(param.getProductCode(), param.getCount());
        if (!stockResult){
            //没有扣减成功 返回 下面不调用了
            return false;
        }
        //2.在扣减库存成功后 新增订单 OrderPO对应order_tbl
        OrderPO po=new OrderPO();
        BeanUtils.copyProperties(param,po);
        int orderResult=orderMapper.insert(po);
        //3 删除购物车 pram productCode 和 userId
        Boolean cartResult=cartService.deleteCart(param.getProductCode(),param.getUserId());
        return true;
    }
}
